<template>
  <el-main>
    <el-row type="flex">
      <el-col :span="2">
        <el-button type="primary" @click="handleFlash">刷新</el-button>
      </el-col>
    </el-row>

    <el-table :data="sliceRP" style="width: 100%" max-height="600">
      <el-table-column label="编号" width="200">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="区域" width="200">
        <template slot-scope="scope">
          {{ scope.row.regionInfo }}
        </template>
      </el-table-column>
      <el-table-column label="坐标" width="200">
        <template slot-scope="scope">
          ({{ scope.row.x }}, {{ scope.row.y }})
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="230">
        <template slot-scope="scope">
          {{ scope.row.updateTime | timeFormate }}
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="showFingerprintInfo(scope.$index, scope.row)"
          >
            查看指纹
          </el-button>
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="referencePoints.length"
    ></el-pagination>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="40%"
      :show-close="true"
      :destroy-on-close="true"
      @close="handleCloseDialog"
    >
      <el-table :data="dialogTable">
        <el-table-column
          property="apMac"
          label="AP-MAC"
        ></el-table-column>
        <el-table-column
          property="mean"
          label="平均值"
          :formatter="meanFormatter"
        ></el-table-column>
        <el-table-column
          property="standardDeviation"
          label="标准差"
          :formatter="stdFormatter"
        ></el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">
          确 定
        </el-button>
      </span>
    </el-dialog>
  </el-main>
</template>

<script>
import dayjs from 'dayjs'
// import axios from 'axios'
import { mapActions, mapState } from 'vuex'
export default {
  name: 'ReferencePointTable',
  data() {
    return {
      dialogVisible: false,
      dialogTitle: '',
      dialogTable: {
        mac: '',
        mean: null,
        standardDeviation: null,
      },
      currentPage: 1,
      pageSize: 10,
    }
  },

  methods: {
    ...mapActions(['getReferencePoints']),
    // 分页操作
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.currentPage = 1
      this.pageSize = val
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
    },
    handleCloseDialog() {
      console.log('closeDialog')
    },
    showFingerprintInfo(index, row) {
      console.log('showFingerprintInfo', index, row)
      console.log(row.fingerprint, typeof row.fingerprint)
      this.dialogTable = JSON.parse(row.fingerprint)
      this.dialogTitle = row.id + '号参考点指纹 - (' + row.x + ', ' + row.y + ')'
      this.dialogVisible = true
    },
    handleEdit(row) {
      console.log('handleEdit', row)
    },
    handleDelete(index, row) {
      console.log('handleDelete', index, row)
    },
    handleFlash() {
      console.log('handleFlash')
    },
    meanFormatter(row, column) {
      return parseFloat(row.mean.toFixed(4))
    },
    stdFormatter(row, column) {
      return parseFloat(row.standardDeviation.toFixed(4))
    },
  },
  computed: {
    ...mapState(['referencePoints']),
    sliceRP() {
      return this.referencePoints.slice(
        (this.currentPage - 1) * this.pageSize,
        this.currentPage * this.pageSize
      )
    },
  },
  filters: {
    // 时间格式化
    timeFormate(value, str = 'YYYY/MM/DD HH:mm:ss') {
      // console.log('@',value)
      if (value != null) {
        return dayjs(value).format(str)
      } else {
        return '无最近记录'
      }
    },
  },
}
</script>

<style lang="less" scoped></style>
